package table

import (
	"context"
	"errors"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type TableAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTableAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TableAddLogic {
	return &TableAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TableAddLogic) TableAdd(req *types.TableAddReq) (resp *types.TableAddResp, err error) {

	resp = new(types.TableAddResp)
	user := new(model.UserModel)
	data := user.TableName()
	if err = l.svcCtx.KqClient.Push(data); err != nil {
		logx.Errorf("KqPusherClient 插入失败, err :%v", err)
		return nil, errors.New("Kq插入失败")
	}
	resp.Message = "kqPusher"

	role := new(model.RoleModel)
	deplayResp, err := l.svcCtx.DqPusherClient.Delay([]byte(role.TableName()), 60*time.Second)
	if err != nil {
		logx.Errorf("延时任务异常: %v", err)
		return nil, err
	}
	logx.Infof("延时任务: %s", deplayResp)
	resp.Message += ",deplay"

	power := new(model.PowerModel)
	atResp, err := l.svcCtx.DqPusherClient.At([]byte(power.TableName()), time.Now())
	if err != nil {
		logx.Errorf("定时任务异常: %v", err)
		return nil, err
	}
	logx.Infof("定时任务: %s", atResp)
	resp.Message += ",at"
	return
}
